package 图2;

import java.util.Arrays;

public class 图2 {
	public static void main(String[] args) {
		
		//测试
		Graph g = new Graph(5);
		g.addVertex(new Vertex("A"));
		g.addVertex(new Vertex("B"));
		g.addVertex(new Vertex("C"));
		g.addVertex(new Vertex("D"));
		g.addVertex(new Vertex("E"));
		
		//添加边
		g.addEdge("A", "B");
		g.addEdge("A", "C");
		g.addEdge("B", "C");
		g.addEdge("B", "D");
		g.addEdge("B", "E");
		
		for(int[] a:g.adjMat) {
			System.out.println(Arrays.toString(a));
		}
	}
}

/**
 * 图
 * @author PC
 *
 */
class Graph{
	
	//存储顶点
	public Vertex[] vertex;
	//统计顶点
	public int currentSize;
	//邻接矩阵
	public int[][] adjMat;
	
	public Graph(int size) {
		//初始化数组
		vertex = new Vertex[size];
		//初始化邻接矩阵
		adjMat = new int[size][size];
	}
	
	/**
	 *  添加结点
	 * @param v
	 */
	public void addVertex(Vertex v) {
		vertex[currentSize++] = v;
	}
	
	/**
	 * 添加边，使用值添加，不是下标
	 * @param v1
	 * @param v2
	 */
	public void addEdge(String s1, String s2) {
		//找到这另个值的下标
		int v1 = 0;
		for (int i = 0; i < vertex.length; i++) {
			if(vertex[i].value.equals(s1)) {
				v1 = i;
				break;
			}
		}
		int v2 = 0; 
		for (int i = 0; i < vertex.length; i++) {
			if(vertex[i].value.equals(s2)) {
				v2 = i;
				break;
			}
		}
		
		//添加边
		adjMat[v1][v2] = 1;
		adjMat[v2][v1] = 1;
		
	}
}

/**
 * 顶点
 * @author PC
 *
 */
class Vertex{
	public String value;

	public Vertex(String value) {
		super();
		this.value = value;
	}



	@Override
	public String toString() {
		return "Vertex [value=" + value + "]";
	}
	
}